<template>
  <div>
    <el-row :gutter="8" style="margin-bottom: 20px">
      <el-col :span="6">
        <el-card>
          <div style="color: #409EFF"><i class="el-icon-user-solid"/> 用户总数</div>
          <div style="padding: 10px 0; text-align: center; font-weight: bold">{{ userNum }}</div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card>
          <div style="color: #67C23A"><i class="el-icon-document"/> 文章总数</div>
          <div style="padding: 10px 0; text-align: center; font-weight: bold">{{ articleNum }}</div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card>
          <div style="color: #E6A23C"><i class="el-icon-chat-line-square"/> 评论总数</div>
          <div style="padding: 10px 0; text-align: center; font-weight: bold">{{ commentNum }}</div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card>
          <div style="color: #F56C6C"><i class="el-icon-magic-stick"/>点赞总数</div>
          <div style="padding: 10px 0; text-align: center; font-weight: bold">{{ likeNum }}</div>
        </el-card>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <div id="main" style="width: 500px; height: 370px"> </div>
      </el-col>
      <el-col :span="12">
        <div id="pie"  style="width: 500px; height: 370px"></div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import * as echarts from 'echarts';
export default {
  name: "Home",
  data() {
    return {
      userNum: 0,
      articleNum: 0,
      likeNum: 0,
      commentNum: 0
    }
  },
  mounted() { //页面元素渲染之后再触发
    var chartDom = document.getElementById('main');
    var myChart = echarts.init(chartDom);
    var option = {
      title: {
        text: '各季度新增数量统计',
        subtext: '趋势图',
        left: 'center'
      },
      tooltip: {          // 显示具体数据
        trigger: 'item'
      },
      legend: {          // 显示颜色代表的信息
        orient: 'vertical',
        right: 10,
        top: 4
      },
      xAxis: {
        type: 'category',
        data: ["第一季度", "第二季度", "第三季度", "第四季度",]
      },
      yAxis: {
        type: 'value'
      },
      series: [
        {
          name: '123',
          data: [],
          type: 'line'
        },
        {
          name: '用户数量',
          data: [],
          type: 'bar'
        },
        {
          name: '文章数量',
          data: [],
          type: 'bar'
        }
      ]
    };
    this.request.get("/echarts/members").then(res => {
      if (res.code === '200'){
      //   // option.xAxis.data = res.data
        option.series[0].data = res.data.user
        option.series[1].data = res.data.user
        option.series[2].data = res.data.article
        // 在数据准备完之后再 set
        myChart.setOption(option);
      }else{
        this.$message.error(res.msg)
      }
    })


    // 饼图
    var pieOption = {
      title: {
        text: '各季度注册数量对比',
        subtext: '比例图',
        left: 'center'
      },
      tooltip: {          // 显示具体数据
        trigger: 'item'
      },
      legend: {          // 显示颜色代表的信息
        orient: 'vertical',
        left: 'left'
      },
      series: [
        {
          name: '用户注册数量',
          type: 'pie',
          // radius: '50%', // 饼图大小
          data: [],
          radius: [20, 110],
          center: ['25%', '50%'],
          roseType: 'radius',
          label:{        //饼图图形上的文本标签
            normal: {
              show: true,
              position: 'inner',
              textStyle: {
                fontWeight: 300,
                fontSize: 12,    //设置文字大小
                color: '#fff'
              },
              formatter: '{d}%'
            }
          },
          emphasis: {
            itemStyle: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: 'rgba(0, 0, 0, 0.5)'
            }
          }
        },
        {
          name: '文章新增数量',
          type: 'pie',
          radius: '40%', // 饼图大小
          data: [],
          // radius: [20, 90],
          center: ['75%', '50%'],
          // roseType: 'radius',
          label:{        //饼图图形上的文本标签
            normal: {
              show: true,
              position: 'inner',
              textStyle: {
                fontWeight: 300,
                fontSize: 12,    //设置文字大小
                color: '#fff'
              },
              formatter: '{d}%'
            }
          },
          emphasis: {
            itemStyle: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: 'rgba(0, 0, 0, 0.5)'
            }
          }
        }
      ]
    };
    var pieDom = document.getElementById('pie');
    var pieChart = echarts.init(pieDom);
    this.request.get("/echarts/members").then(res => {
      if (res.code === '200'){
        pieOption.series[0].data = [
          {name: "第一季度", value: res.data.user[0]},
          {name: "第二季度", value: res.data.user[1]},
          {name: "第三季度", value: res.data.user[2]},
          {name: "第四季度", value: res.data.user[3]},
        ]
        pieOption.series[1].data = [
          {name: "第一季度", value: res.data.article[0]},
          {name: "第二季度", value: res.data.article[1]},
          {name: "第三季度", value: res.data.article[2]},
          {name: "第四季度", value: res.data.article[3]},
        ]
        pieChart.setOption(pieOption)
        this.userNum = res.data.userNum
        this.articleNum = res.data.articleNum
        this.likeNum = res.data.likeNum
        this.commentNum = res.data.commentNum
      }else{
        this.$message.error(res.msg)
      }
    })
  }

}
</script>
<style scoped>

</style>